package com.library.workbench.web.controller.user;

import com.library.commons.constant.Constant;
import com.library.commons.domain.ReturnObject;
import com.library.settings.pojo.Admin;
import com.library.settings.pojo.User;
import com.library.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserInfoController {
    @Autowired
    private UserService userService;

    @RequestMapping("/workbench/user/person/toUserInfo.do")
    public String toUserInfo(HttpSession session, HttpServletRequest request){
        // 查询管理员信息
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        // 放入request域
        request.setAttribute("user",user);
        return "workbench/user/person/personInfor";
    }

    @RequestMapping("/workbench/user/person/updateUserInfo.do")
    @ResponseBody
    public Object updateAdminInfo(User user){
        ReturnObject returnObject=new ReturnObject();
        // 调用service层方法
        try {
            int res = userService.updateUser(user);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else {
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }


    @RequestMapping("/workbench/user/exit.do")
    public String exit(HttpSession session){
        // 销毁session
        session.invalidate();
        // 返回登录界面
        return "settings/qx/user/login";
    }
}
